package com.yyt.guavademo.model;

import com.google.common.cache.CacheBuilder;
import com.google.common.cache.CacheLoader;
import com.google.common.cache.LoadingCache;

import java.time.Duration;

/**
 * @author yeyangtao
 * @since 2022/7/21 15:34
 */
public class UserCache {
    public static LoadingCache<Integer, User> userCache = CacheBuilder.newBuilder()
            // 最大容量
            .maximumSize(100)
            // 过期时间
            .expireAfterAccess(Duration.ofMinutes(30))
            .build(new CacheLoader<Integer, User>() {
                @Override
                public User load(Integer userId) throws Exception {
                    return getUserFromDb(userId);
                }
            });

    private static User getUserFromDb(Integer userId) {
        return User.builder().id(1).name("yyt").passwd("123").build();
    }
}
